package cn.wiseda.finance.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.wiseda.finance.dao.util.page.Page;
import cn.wiseda.finance.dao.util.page.Pager;
import cn.wiseda.finance.entity.Bank;
import cn.wiseda.finance.entity.CashFlow;
import cn.wiseda.finance.services.BankServices;
import cn.wiseda.finance.services.CashFlowServices;

@Controller
@RequestMapping("cashflow")
public class CashFlowController {

	@Resource
	private BankServices bankServices;

	@Resource
	private CashFlowServices cashFlowServices;

	@RequestMapping("list.jhtml")
	public String cashflow(@RequestParam(required = false, defaultValue = "0") int bankId, Model model) {
		List<Bank> banks = bankServices.getBanks();
		model.addAttribute("banks", banks);
		if (bankId != 0) {
			List<CashFlow> listCashFlow = cashFlowServices.listCashFlowByBank(bankId);
			model.addAttribute("cf", listCashFlow);
			System.out.println(listCashFlow);
			model.addAttribute("bankId", bankId);
		}
		return "cashflow/list";
	}

	@RequestMapping("page.jhtml")
	public String cashflowByPage(@RequestParam(required = false, defaultValue = "0") int bankId,
			@RequestParam(required = false, defaultValue = "1") int pageNo,@RequestParam(required = false, defaultValue = "20") int pageSize, Model model) {
		List<Bank> banks = bankServices.getBanks();
		model.addAttribute("banks", banks);
		if (bankId != 0) {
			Pager<CashFlow> listCashFlow = cashFlowServices.pageCashFlowByBank(bankId, pageNo, pageSize);
			model.addAttribute("cf", listCashFlow);
			model.addAttribute("bankId", bankId);
		}
		return "cashflow/page";
	}

	// pageCashFlowByBank

	@RequestMapping("create.jhtml")
	public String create(Model model) {
		List<Bank> banks = bankServices.getBanks();
		model.addAttribute("banks", banks);
		return "cashflow/create";
	}

	@RequestMapping("insert.jhtml")
	public String insertCashFlow(CashFlow cf) {
		if (cf.getDebit() == null)
			cf.setDebit(0f);
		if (cf.getCreditor() == null)
			cf.setCreditor(0f);

		cashFlowServices.insertCashFlow(cf);
		return "redirect:list.jhtml?bankId=" + cf.getBankId();
	}

	@RequestMapping("test.jhtml")
	public String ddd(@RequestParam(required = false, defaultValue = "0") int bankId, Model model) {

		List<Bank> banks = bankServices.getBanks();
		model.addAttribute("banks", banks);
		if (bankId != 0) {
			List<CashFlow> settleAccounts = cashFlowServices.settleAccounts(bankId);
			model.addAttribute("cf", settleAccounts);
			model.addAttribute("bankId", bankId);
		}
		return "cashflow/list1";
	}

	@RequestMapping("settle.jhtml")
	public String settleAccountsByBank(@RequestParam(required = false, defaultValue = "0") int bankId, Model model) {
		System.out.println("BankID:" + bankId);
		List<Bank> banks = bankServices.getBanks();
		model.addAttribute("banks", banks);
		if (bankId != 0) {
			try {
				cashFlowServices.settleAccountsByBank(bankId);
			} catch (Exception e) {
				model.addAttribute("msg", "<div class=\"alert alert-danger\"><strong>错误!</strong>结算失败!！</div>");
				return "cashflow/list1";
			}
		}
		model.addAttribute("msg", "<div class=\"alert alert-info\"><strong>成功!</strong>结算完成!！</div>");
		return "cashflow/list1";
	}
}
